package com.next.aappublicapp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.Settings;
import com.google.analytics.tracking.android.EasyTracker;
import com.next.aap.client.AllItem;
import com.next.aap.client.NewsItem;
import com.next.aap.client.VideoItem;
import com.next.aappublicapp.handlers.ItemClickHandlerImpl;
import com.next.aappublicapp.handlers.ItemListClickHandler;
import com.next.aappublicapp.loaders.AllItemsLoader;
import com.next.aappublicapp.util.FacebookUtil;
import com.next.aappublicapp.util.NotificationUtil;
import com.next.aappublicapp.util.PreferencesUtil;
import com.next.aappublicapp.views.ImageTextScrollerView;

public class MainActivity extends BaseFragmentActivty implements
		LoaderCallbacks<AllItem> {

	private ShareActionProvider mShareActionProvider;

	private ImageTextScrollerView<NewsItem> newsItemScrollerView;
	// private ImageTextScrollerView<NewsItem> peopleNewsItemScrollerView;
	// private ImageTextScrollerView<BlogItem> blogScrollerView;
	private ImageTextScrollerView<VideoItem> videoScrollerView;
	// private ImageTextScrollerView<EventItem> eventScrollerView;

	private ImageButton facebookFeedButton;
	private ImageButton twitterFeedButton;
	private ImageButton delhiElectionButton;
	private ImageButton donationButton;

	private TextView delhiElectionTextView;
	private TextView donationTextView;
	private ImageButton swarajButton;
	private ImageButton janlokpalButton;

	private AllItemsLoader allItemsLoader;
	private LinearLayout loadingStatusView;

	private AllItem allItem;

	@Override
	protected void onResume() {
		super.onResume();
		Settings.publishInstallAsync(this, "179991405492511");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		NotificationUtil.clearAllNotifications(this);

		EasyTracker.getTracker().sendView("Main Screen");

		Log.i("Ravi", "Main Activity Started");
		setContentView(R.layout.activity_main);

		FacebookUtil.publishFirstTimeInstallMessage(this);

		facebookFeedButton = (ImageButton) findViewById(R.id.facebookFeedButton);
		twitterFeedButton = (ImageButton) findViewById(R.id.twitterFeedButton);
		delhiElectionButton = (ImageButton) findViewById(R.id.delhiElectionButton);
		delhiElectionTextView = (TextView) findViewById(R.id.delhiElectionTextView);
		donationButton = (ImageButton) findViewById(R.id.donationButton);
		donationTextView = (TextView) findViewById(R.id.donationTextView);
		// publish();
		swarajButton = (ImageButton) findViewById(R.id.swarajButton);
		janlokpalButton = (ImageButton)findViewById(R.id.janlokpalButton);
		loadingStatusView = (LinearLayout) findViewById(R.id.loading_status);

		facebookFeedButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						FacebookFeedActivity.class);
				MainActivity.this.startActivity(intent);
				EasyTracker.getTracker().sendEvent("Android", "Social",
						"FacebookFeedButton", 1L);

			}
		});

		swarajButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String hindi = MainActivity.this.getString(R.string.Hindi);
				final CharSequence[] items = {hindi, "English"};

				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("Choose a Language");
				builder.setItems(items, new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface dialog, int item) {
				        //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
				        Intent intent = new Intent(MainActivity.this,SwarajActivity.class);
				        intent.putExtra("Language", items[item]);
						MainActivity.this.startActivity(intent);
				    }
				});
				AlertDialog alert = builder.create();
				alert.show();
				
				

			}
		});
		
		janlokpalButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						JanlokPalActivity.class);
				Bundle bundle = new Bundle();
				MainActivity.this.startActivity(intent);

			}
		});
		
		twitterFeedButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(MainActivity.this,
						"Under Construction", duration);
				toast.show();
				EasyTracker.getTracker().sendEvent("Android", "Social",
						"TwitterFeedButton", 1L);
				/*
				 * Intent intent = new Intent(MainActivity.this,
				 * TwitterFeedActivity.class); Bundle bundle = new Bundle();
				 * MainActivity.this.startActivity(intent);
				 */
			}
		});

		View.OnClickListener delhiElectionListner = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						StateElectionMainActivity.class);
				MainActivity.this.startActivity(intent);
				EasyTracker.getTracker().sendEvent("Android", "StateElection",
						"Delhi", 1L);

			}
		};
		delhiElectionButton.setOnClickListener(delhiElectionListner);
		delhiElectionTextView.setOnClickListener(delhiElectionListner);

		View.OnClickListener donationListner = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						DonationSummaryActivity.class);
				MainActivity.this.startActivity(intent);
				EasyTracker.getTracker().sendEvent("Android", "Donation",
						"Yes", 1L);

			}
		};
		donationButton.setOnClickListener(donationListner);
		donationTextView.setOnClickListener(donationListner);

		LinearLayout mainView = (LinearLayout) findViewById(R.id.mainView);

		ItemListClickHandler<NewsItem> newsItemClickedHandler = new ItemClickHandlerImpl<NewsItem>(
				this);
		newsItemScrollerView = new ImageTextScrollerView<NewsItem>(
				super.getBaseContext(), R.string.party_news_heading,
				newsItemClickedHandler);
		mainView.addView(newsItemScrollerView);

		// peopleNewsItemScrollerView = new
		// ImageTextScrollerView<NewsItem>(super.getBaseContext(),
		// R.string.people_news_heading,newsItemClickedHandler);
		// mainView.addView(peopleNewsItemScrollerView);

		// blogScrollerView = new
		// ImageTextScrollerView<BlogItem>(super.getBaseContext(),
		// R.string.blog_heading,this);
		// mainView.addView(blogScrollerView);

		ItemListClickHandler<VideoItem> videoItemClickedHandler = new ItemClickHandlerImpl<VideoItem>(
				this);
		videoScrollerView = new ImageTextScrollerView<VideoItem>(
				super.getBaseContext(), R.string.video_heading,
				videoItemClickedHandler);
		mainView.addView(videoScrollerView);

		// ItemListClickHandler<EventItem> eventItemClickedHandler = new
		// ItemClickHandlerImpl<EventItem>(this);
		// eventScrollerView = new
		// ImageTextScrollerView<EventItem>(super.getBaseContext(),
		// R.string.event_heading,eventItemClickedHandler);
		// mainView.addView(eventScrollerView);

		this.allItem = PreferencesUtil.getInstance().getAllItemCache(this);
		loadItems(allItem);

		allItemsLoader = new AllItemsLoader(this);
		Bundle bundle = null;

		getSupportLoaderManager().initLoader(0, bundle, this);

		// Check if We need to forward to new Activity

		if (getIntent().getExtras() == null) {
			Log.i("Ravi", "No Extra Data Provided with Intent");

		} else {

			Log.i("Ravi", "Extra Data Provided with Intent");
			Bundle bundle1 = getIntent().getExtras().getBundle(
					"NotificationMessage");
			Log.i("Ravi", "bundle=" + bundle1);
			if (bundle1 != null) {
				boolean forward = bundle1.getBoolean("Forward", false);
				String msgType = bundle1.getString("msgType");
				if (msgType != null) {
					msgType = "No MSG Type";
				}
				EasyTracker.getTracker().sendView("Notfication " + msgType);
				Log.i("Ravi", "forward=" + forward);
				if (forward) {
					Class<Activity> forwardActivity = (Class<Activity>) bundle1
							.getSerializable("ForwardActivity");
					if (forwardActivity != null) {
						Intent intent = new Intent(MainActivity.this,
								forwardActivity);
						intent.putExtra("Forward Bundle", bundle1);
						MainActivity.this.startActivity(intent);
					}
				}

			} else {
				EasyTracker.getTracker()
						.sendView("Notfication With No Message");
			}

		}

	}

	/*
	 * @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	 * 
	 * @Override public boolean onCreateOptionsMenu(Menu menu) {
	 * getMenuInflater().inflate(R.menu.main, menu);
	 * 
	 * // Locate MenuItem with ShareActionProvider MenuItem item =
	 * menu.findItem(R.id.menu_item_share); // Fetch and store
	 * ShareActionProvider mShareActionProvider = (ShareActionProvider)
	 * item.getActionProvider(); return true; }
	 */
	// Call to update the share intent
	private void setShareIntent(Intent shareIntent) {
		if (mShareActionProvider != null) {
			mShareActionProvider.setShareIntent(shareIntent);
		}
	}

	@Override
	public Loader<AllItem> onCreateLoader(int id, Bundle args) {
		loadingStatusView.setVisibility(View.VISIBLE);
		return allItemsLoader;
	}

	@Override
	public void onLoadFinished(Loader<AllItem> arg0, AllItem allItem) {
		loadingStatusView.setVisibility(View.GONE);
		this.allItem = allItem;
		loadItems(allItem);

	}

	private void loadItems(AllItem allItem) {
		if (allItem == null) {
			// newsItemScrollerView.setItems(new ArrayList<NewsItem>());
			// peopleNewsItemScrollerView.setItems(new ArrayList<NewsItem>());
			// videoScrollerView.setItems(new ArrayList<VideoItem>());
			// blogScrollerView.setBlogItem(allItem.getBlogItems());
			// eventScrollerView.setItems(new ArrayList<EventItem>());
		} else {
			newsItemScrollerView.setItems(allItem.getNewsItems());
			// peopleNewsItemScrollerView.setItems(allItem.getNewsItems());
			videoScrollerView.setItems(allItem.getVideoItems());
			// blogScrollerView.setBlogItem(allItem.getBlogItems());
			// eventScrollerView.setItems(allItem.getEventItems());
		}
	}

	@Override
	public void onLoaderReset(Loader<AllItem> arg0) {

	}

}
